<template>
  <div class="search-box box-shadow-card">
    <div class="search-box-content">
      <div class="search">
        <input
            class="search-input"
            type="text"
            placeholder="请输入要搜索的文章名"
            v-model="searchText"
            @keyup.enter="handleSearch"
        />
        <div class="wrap-history">
          <div class="left"></div>
          <div class="right"></div>
        </div>
      </div>
      <button class="search-button" @click="handleSearch">
        Z搜索
      </button>
    </div>

    <div class="search-history" v-if="searchHistory.length">
      <div class="history-item" v-for="(item, index) in searchHistory" :key="index" @click="reuseSearch(item)">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref} from 'vue';

const searchText = ref('');
const searchHistory = ref<string[]>([]); // 存储搜索历史

function handleSearch() {
  const text = searchText.value.trim();
  if (text === '') {
    // ElMessage.warning('请输入搜索内容');
    return;
  }

  // 添加到搜索历史（如果不存在）
  if (!searchHistory.value.includes(text)) {
    searchHistory.value.push(text);
  }

  // 跳转到搜索结果页面
  // const searchQuery = encodeURIComponent(text);
  // window.location.href = `/search/result?query=${searchQuery}`;

  // 清空输入框
  searchText.value = '';
}

function reuseSearch(term: string) {
  searchText.value = term;
  handleSearch(); // 重新执行搜索
}
</script>

<style scoped lang="scss">
.search-box {
  width: 100%;

  padding: 8px;
  overflow: visible;

  .search-box-content {
    display: flex;
    height: 48px;

    .search {
      width: 100%;
      height: 100%;
      position: relative;

      .search-input {
        width: 100%;
        height: 100%;
        padding: 0 16px;
        font-size: 16px;
        border: 1px solid #ccc;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        outline: none;
        z-index: 0;

        &::placeholder {
          color: #999;
        }

        &:hover {
          border: 1px solid $blog-primary-light-2;
          //box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        }

        &:focus {
          border: 1px solid $blog-primary-light-2;
          border-bottom: 1px solid transparent;
          border-bottom-left-radius: 0;
          //box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          /* 将 wrap-history 展示出来*/
          + .wrap-history {
            display: block;
          }
        }
      }

      .wrap-history {
        display: none;
        z-index: 1;
        position: absolute;
        height: 360px;
        width: 100%;
        background-color: white;
        border-left: 1px solid $blog-primary-light-2;
        border-right: 1px solid $blog-primary-light-2;
        border-bottom: 1px solid $blog-primary-light-2;
        border-top: none;
        /*左下角右下角圆角*/
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;

        &::after {
          content: '';
          position: absolute;
          bottom: 100%;
          width: 98%;
          margin: 0 8px;
          border-top: 2px solid #EEEEEE;
        }
      }
    }

    .search-button {
      width: 168px;
      height: 100%;
      background-color: $blog-primary-light-2;
      color: white;
      font-size: 16px;
      font-weight: bold;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
    }

  }

  .search-history {
    margin-top: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;

    .history-item {
      padding: 4px 8px;
      background-color: #f5f5f5;
      border-radius: 4px;
      cursor: pointer;

      &:hover {
        background-color: #eaeaea;
      }
    }
  }
}
</style>
